﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;

namespace TcpDownServer
{
    class Logger
    {
        public static string FilePath;
        public static int Mode;

        public static void Info(string message)
        {
            if (Mode > 0)
            {
                WriteFile("[info:" + DateTime.Now.ToString() + "]" + message + "\r\n");
            }
        }
        public static void Debug(string message)
        {
            if (Mode > 1)
            {
                WriteFile("[debug:" + DateTime.Now.ToString() + "]" + message + "\r\n");
            }
        }
        public static void Error(string message)
        {
            WriteFile("[error:" + DateTime.Now.ToString() + "]" + message + "\r\n");
        }
        private static void WriteFile(string Txt)
        {
            if (!File.Exists(FilePath))
            {
                using (FileStream fs = File.Create(FilePath))
                {
                    fs.Close();
                }
            }
            byte[] data=Encoding.UTF8.GetBytes(Txt);
            ThreadPool.QueueUserWorkItem(
                (WaitCallback)delegate(object state)
            {
                using (FileStream fs = new FileStream(FilePath, FileMode.Append, FileAccess.Write, FileShare.Write))
                {
                    fs.Write((byte[])data, 0, ((byte[])data).Length);
                    fs.Flush();
                    fs.Close();
                }
            }
            , data);
        }
    }
}
